package com.zcp.controller;

import com.zcp.entity.Student;
import com.zcp.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("student")

public class StudentController {
    @Autowired
    private StudentService  studentService;



    @RequestMapping("save")
    public String save(Student Student){
        studentService.save(Student);
        return "redirect:/student/findAll";
    }

    @RequestMapping("findAll")
    public String findAll(Integer pageNow,Integer rows, String searchCol,String searchValue, Model model){
        pageNow = pageNow==null?1:pageNow;
        rows = rows==null?9:rows;
        List<Student> students = studentService.finaAll(pageNow,rows,searchCol,searchValue);
        //得到条数
        int counts = studentService.totalCount(searchCol,searchValue);
        //得到页数
        Integer totalpage = counts%rows==0?(counts/rows):((counts/rows)+1);
        model.addAttribute("students",students);
        model.addAttribute("searchValue",searchValue);
        model.addAttribute("searchCol",searchCol);
        model.addAttribute("totalpage",totalpage);
        model.addAttribute("pageNow",pageNow);
        return "back/student/index";
    }
    @GetMapping("change/{id}")
    public String findAll(@PathVariable("id") Integer id, Model model){
        Student student = studentService.change(id);
        model.addAttribute("student",student);
        return "back/student/change";
    }

    @RequestMapping("changed")
    public String changeed(Student Student){
        studentService.changed(Student);
        return "redirect:/student/findAll";
    }

    @GetMapping("delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        System.out.println(id);
        studentService.delete(id);
        return "redirect:/student/findAll";
    }
}
